package com.wudizaba.microservicesdemosystem.service.impl;

import com.wudizaba.microservicesdemocommon.Exceptions.ExcelException;
import com.wudizaba.microservicesdemocommon.constant.FileNameConstant;
import com.wudizaba.microservicesdemocommon.constant.ExceptionsConstant;
import com.wudizaba.microservicesdemosystem.mapper.ProduceCraftMapper;
import com.wudizaba.microservicesdemosystem.module.vo.ExcelProduceVO;
import com.wudizaba.microservicesdemosystem.module.vo.ProduceAllVO;
import com.wudizaba.microservicesdemosystem.module.vo.ProduceProcessVO;
import com.wudizaba.microservicesdemosystem.service.IExcelService;
import com.wudizaba.microservicesdemosystem.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class IExcelServiceImpl implements IExcelService {

    @Autowired
    private ProduceCraftMapper  produceCraftMapper;

    @Override
    public void exportProduceExcel(HttpServletResponse response) {
        //查询所有
        List<ProduceAllVO> produceAllVOList = produceCraftMapper.selectAllProduce();


        if (CollectionUtils.isEmpty(produceAllVOList)) {
            throw new ExcelException(ExceptionsConstant.EXCEL_IS_EMPTY_BY_EXPORT);
        }

        List<ExcelProduceVO> resultList = new ArrayList<>();

        for (ProduceAllVO produceAllVO : produceAllVOList) {
            List<ProduceProcessVO> produceProcessesList = produceAllVO.getProduceProcessesList();
            String sequence = produceProcessesList.stream().map(p -> p.getSequence().toString()).collect(Collectors.joining(","));
            String description = produceProcessesList.stream().map(p -> p.getProcessDescription()).collect(Collectors.joining(","));
            ExcelProduceVO excelProduceVO = new ExcelProduceVO();
            excelProduceVO.setProduceStationStationName(produceAllVO.getStationName());
            excelProduceVO.setProduceStationLocation(produceAllVO.getLocation());
            excelProduceVO.setProduceStationStatus(produceAllVO.getStatus().toString());
            excelProduceVO.setProduceCraftCraftName(produceAllVO.getCraftName());
            excelProduceVO.setProduceCraftCraftType(produceAllVO.getCraftType());
            excelProduceVO.setProduceCraftDescription(produceAllVO.getCraftDescription());
            excelProduceVO.setProduceProcessSequence(sequence);
            excelProduceVO.setProduceProcessDescription(description);
            excelProduceVO.setUpdateTime(produceAllVO.getUpdateTime());
            resultList.add(excelProduceVO);
        }


        //文件名称
        String fileName = FileNameConstant.EXCEL_FILE_NAME_PRODUCE_STATION + System.currentTimeMillis();

        ExcelUtil.write(response, ExcelProduceVO.class, fileName, FileNameConstant.EXCEL_FILE_NAME_PRODUCE_STATION, resultList);


    }
}
